package com.ruoyi.web.sso.controller;



import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.sso.domain.Staff;
import com.ruoyi.web.sso.domain.DTO.StatusResponse;
import com.ruoyi.web.sso.domain.vo.*;
import com.ruoyi.web.sso.service.IRedisUtilService;
import com.ruoyi.web.sso.service.IStaffService;
import com.ruoyi.web.sso.util.DateUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import javax.servlet.http.HttpServletRequest;
import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-10-20
 */
@Controller
@RequestMapping("/sso/staff")
@Api(tags = "用户信息")
public class StaffController extends BaseController {

    @Autowired
    private IStaffService staffService;
    @Autowired
    private IRedisUtilService redisService;

    @RequestMapping(value="/create",method= RequestMethod.POST)
    @ApiOperation(value="创建用户",notes="创建用户")
    @ResponseBody
    public AjaxResult create(@RequestBody CreateStaff createStaff)
    {
        return staffService.create(createStaff);
    }

    @RequestMapping(value="/create1",method= RequestMethod.POST)
    @ApiOperation(value="创建用户",notes="创建用户")
    @ResponseBody
    public AjaxResult create1(String mobile, String password, String username, String realName,String gender)
    {
        CreateStaff createStaff=new CreateStaff();
        createStaff.setPassword(password);
        createStaff.setMobile(mobile);
        createStaff.setUsername(username);
        createStaff.setRealName(realName);
        createStaff.setGender(StringUtils.isEmpty(gender)?2:Long.parseLong(gender));
        return staffService.create(createStaff);
    }



    @RequestMapping(value="/getTicket",method= RequestMethod.POST)
    @ApiOperation(value="获取Ticket",notes="获取Ticket")
    @ResponseBody
    public String getTicket(String staffId, String platformId)
    {
        return redisService.getTicket(staffId,platformId);
    }

    @RequestMapping(value="/login",method= RequestMethod.POST)
    @ApiOperation(value="登录",notes="登录")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = StaffDetails.class),
    })
    @ResponseBody
    public StatusResponse login(@RequestBody Login login, HttpServletRequest request)
    {
        return staffService.login(login,request);
    }

    /**
     * 查询用户信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value="查询用户信息列表",notes="查询用户信息列表")
    public TableDataInfo list(Staff staff)
    {
        startPage();
        List<Staff> list = staffService.selectStaffList(staff);
        return getDataTable(list);
    }



    /**
     * 查询已分配用户角色列表
     */
    @PostMapping("/allocatedList")
    @ResponseBody
    @ApiOperation(value="查询已分配用户角色列表",notes="查询已分配用户角色列表")
    public TableDataInfo allocatedList(@Validated AllocatedStaff allocatedStaff)
    {
        startPage();
        List<Staff> list = staffService.selectAllocatedList(allocatedStaff);
        return getDataTable(list);
    }

    @PostMapping("/selectUnallocatedList")
    @ResponseBody
    @ApiOperation(value="查询用户信息列表",notes="查询用户信息列表")
    public TableDataInfo selectUnallocatedList(@ApiParam(value = "角色ID")@RequestParam(required=false) String roleId,
                                               @ApiParam(value = "账号")@RequestParam(required=false) String mobile)
    {
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId);
        map.put("mobile",mobile);

        startPage();
        List<Staff> list = staffService.selectUnallocatedList(map);
        return getDataTable(list);
    }

    /**
     * 查询用户信息列表
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    @ApiOperation(value="查询用户信息列表",notes="查询用户信息列表")
    public AjaxResult changeStatus(Staff staff)
    {
        return staffService.changeStatus(staff);
    }

    /**
     * 重置密码
     */
    @PostMapping("/updatePassword")
    @ResponseBody
    @ApiOperation(value="重置密码",notes="重置密码")
    public AjaxResult updatePassword(Staff staff)
    {
        return staffService.updatePassword(staff);
    }


    /**
     * 查询已分配园区角色列表
     */
    @PostMapping("/allAreaList")
    @ResponseBody
    @ApiOperation(value="查询已分配园区角色列表",notes="查询已分配园区角色列表")
    public TableDataInfo allAreaList(String areaId,String mobile)
    {
        startPage();
        List<Map<String,Object>>  list = staffService.getAllStaffByAreaId(areaId,mobile);
        if (list.size() == 0 || Objects.isNull(list.get(0))){
            list = new ArrayList<>();
        }
        return getDataTable(list);
    }



    @PostMapping("/staffIdByRoleList")
    @ApiOperation(value="用户角色列表",notes="用户角色列表")
    @ResponseBody
    public TableDataInfo staffIdByRoleList(String id,String roleName)
    {
        startPage();
        List<Map<String,Object>> list = staffService.staffIdByRoleList(id, roleName);
        return getDataTable(list);
    }

    @PostMapping("/staffIdByAreaList")
    @ApiOperation(value="用户园区列表",notes="用户园区列表")
    @ResponseBody
    public TableDataInfo staffIdByAreaList(String id,String areaName)
    {
        startPage();
        List<AreaVO>  list= staffService.staffIdByAreaList(id,areaName);
        return getDataTable(list);
    }
    /**
     * 修改保存用户信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Staff staff)
    {
        return toAjax(staffService.updateStaff(staff));
    }


}
